﻿Imports System.Timers
Imports HslCommunication
Imports HslCommunication.Profinet.Siemens
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class UCtmjInfo

    Public Ip As String

    '西门子1200对象
    Public StripperPLC_UC As SiemensS7Net
    'PLC是否连接成功
    Public StripperPlcConFLG_UC As Boolean = False

    Public agv As String = String.Empty

    Dim httphelper As New HttpHelper

    Public lan1 As HslControls.HslLanternSimple
    Public lbl As Label
    Private Heart As Boolean = True

    Public reset_last_flg As Boolean = False
    Public reset_start_time As DateTime
    Public WithEvents tmPLC As New System.Timers.Timer(1000)
    Private Sub tmPLC_Elapsed(sender As Object, e As ElapsedEventArgs) Handles tmPLC.Elapsed
        'Threading.Thread.Sleep(100)
        ' Dim rtn As String = readStripperPlc("DB47.DBX0.4") '通讯检测（读取）
        StripperPlcConFLG_UC = StripperPLC_UC.ReadBool("DB47.DBX0.4").IsSuccess
        If StripperPlcConFLG_UC Then '通讯检测（读取）
            lblConnPlcRtn.Text = "通讯中......"
            Dim bbxx As Boolean = writeStripperBoolPlc("DB47.DBX11.2", True)
            '异常状态（读取）
            Dim rtn00 As String = readStripperPlc("DB1.1.11")
            lblErrorRtn.Text = rtn00
            'Threading.Thread.Sleep(500)
            '脱模机忙闲状态（读取）
            Dim rtn11 As String = readStripperPlc("DB47.DBX0.0")
            lblStatusRtn.Text = rtn11
            'Threading.Thread.Sleep(500)
            '监听AGV到位状态（读取）
            Dim bb1 As String = readStripperPlc("DB47.DBX11.0")
            lblAgvArrvel1Rtn.Text = bb1
            'Threading.Thread.Sleep(500)
            '监听支座型号（读取）
            Dim bb2 As String = readStripperByte("DB47.DBX10", StripperPLC_UC)
            lblsmRtn.Text = bb2
            'Threading.Thread.Sleep(500)
            '监听脱模机拿取支座完成状态（读取）
            Dim rtn1 As String = readStripperPlc("DB47.DBX0.1")
            lblPickRtn.Text = rtn1
            'If rtn1 = True.ToString() Then
            '    If String.IsNullOrEmpty(agv) = False Then
            '        lblMsgTxt.Text = String.Empty
            '        Dim rtnmsg As String = setTask(agv, 1)
            '        If rtnmsg.Equals("ok") Then
            '            writeStripperBoolPlc("DB47.DBX11.0", False)
            '            lblMsgTxt.ForeColor = Color.Green
            '            lblMsgTxt.Text = "执行AGV启成功"
            '        Else
            '            lblMsgTxt.ForeColor = Color.Red
            '            lblMsgTxt.Text = "执行AGV启失败"
            '        End If
            '    Else
            '        lblMsgTxt.ForeColor = Color.Red
            '        lblMsgTxt.Text = "请选择AGV"
            '    End If

            'End If
            'Threading.Thread.Sleep(500)
            '监听AGV到位状态（读取）
            Dim bb3 As String = readStripperPlc("DB47.DBX11.3")
            lblAgvArrvel2Rtn.Text = bb3
            'Threading.Thread.Sleep(500)
            '监听脱模机脱模完成状态（读取）
            Dim rtn2 As String = readStripperPlc("DB47.DBX0.2")
            lblDownRtn.Text = rtn2
            'If rtn2 = True.ToString() Then
            '    If String.IsNullOrEmpty(agv) = False Then
            '        Dim b1xc As Boolean = writeStripperBoolPlc("DB47.DBX11.1", True)
            '        If b1xc Then
            '            Dim rtnmsg As String = setTask(agv, 1)
            '            If rtnmsg.Equals("ok") Then
            '                writeStripperBoolPlc("DB47.DBX11.0", False)
            '                writeStripperBoolPlc("DB47.DBX11.3", False)
            '                lblMsgTxt.ForeColor = Color.Green
            '                lblMsgTxt.Text = "执行AGV启成功"
            '            Else
            '                lblMsgTxt.ForeColor = Color.Red
            '                lblMsgTxt.Text = "执行AGV启失败"
            '            End If
            '            reset_last_flg = True
            '            reset_start_time = Now()
            '        End If
            '    Else
            '        lblMsgTxt.ForeColor = Color.Red
            '        lblMsgTxt.Text = "请选择AGV"
            '    End If
            'End If

            '监听AGV离开状态（读取）
            Dim bbb1 As String = readStripperPlc("DB47.DBX11.1")
            lblLeaveRtn.Text = bbb1
            lan1.LanternBackground = Color.LimeGreen
            If hslAlarm.LightColor = Color.LimeGreen Then
                hslAlarm.LightColor = Color.LightCyan
                'Me.Refresh()
            Else
                hslAlarm.LightColor = Color.LimeGreen
                'Me.Refresh()
            End If
            lbl.Text = "连接结果成功"

            If reset_last_flg = True Then
                If Math.Abs(DateDiff(DateInterval.Second, reset_start_time, Now)) >= 12 Then
                    writeStripperBoolPlc("DB47.DBX11.1", False)
                    reset_last_flg = False
                End If

            End If
        Else

            lan1.LanternBackground = Color.Gray
            lbl.Text = "连接失败！"
            lblConnPlcRtn.Text = "通讯失败，请检查网络！"
            hslAlarm.AlarmColor = Color.Red
            'Application.Restart()
            StripperPLC_UC.ConnectServer()
        End If
    End Sub

    Private Function setTask(agv As String, oper As Integer) As String
        Dim url As String
        url = "http://172.16.21.233/AGVAPI?do=SetAGVState&agv=" & agv & "&oper=" & oper
        Dim msg As String
        Try
            Dim rtn As String = httphelper.HttpGet(url, "")
            Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
            Dim code As String = jo("code").ToString() '输出0
            msg = jo("msg").ToString() '输出ok
        Catch ex As Exception
            msg = "NG"
        End Try

        Return msg
    End Function

    Private Function readStripperPlc(place As String) As String
        '执行plc读取操作
        'If StripperPlcConFLG_UC Then
        Return readStripper(place, StripperPLC_UC)
        'Else
        '    Return String.Empty
        'End If
    End Function

    Private Function readStripper(place As String, plc As SiemensS7Net) As String
        '	RGV当前状态	Int	2.0
        Dim status As String = plc.ReadBool(place).Content
        Return status
    End Function
    Private Function readStripperByte(place As String, plc As SiemensS7Net) As String
        '	RGV当前状态	Int	2.0
        Dim status As String = plc.ReadByte(place).Content
        Return status
    End Function
    Private Function writeStripperBoolPlc(place As String, val As Boolean) As Boolean
        'If StripperPlcConFLG_UC Then
        Return WriteStripperBool(place, val, StripperPLC_UC)
        'Else
        '    Return False
        'End If
    End Function
    Private Function WriteStripperBool(place As String, val As Boolean, plc As SiemensS7Net) As Boolean
        '查询脱模机状态等条件
        'Dim dt As DataTable = objStripper.getList(strippername)
        'If dt.Rows(0)("status").ToString().Equals("1") Then
        '    '如果满足条件 则进行更新
        'End If
        Dim OperR As OperateResult = plc.Write(place, val)
        Return OperR.IsSuccess
    End Function

End Class
